home *** CD-ROM | disk | FTP | other *** search
/ Best of www.BestZips.com (Collector's Edition) / Best of WWW.BESTZIPS.COM Collector's Edition (JCSM Shareware) (JCS Marketing).ISO / editors_ / ze16v250.zip / CINC.CP_ / CINC.CP
Text File  |  1996-01-17  |  11KB  |  463 lines

  1. //-------------------------------------------------------------------------
  2. //--
  3. //-- Copyright (c) 1991-95   Xidicone Pty Ltd
  4. //-- All rights reserved.
  5. //--
  6. //-------------------------------------------------------------------------
  7.  
  8. #include <stdio.h>
  9. #include <direct.h>
  10. #include <stdlib.h>
  11. #include <search.h>
  12. #include <string.h>
  13. #include <ctype.h>
  14.  
  15. //-------------------------------------------------------------------------
  16.  
  17. #define BOOL int
  18.  
  19. #define TRUE  1
  20. #define FALSE 0
  21.  
  22. //-------------------------------------------------------------------------
  23.  
  24. static int  sIndent = 0;
  25.  
  26. static char szIndent[20];
  27. static char szFullName[180];
  28. static char szLineTemp[120];
  29.  
  30. static BOOL fFormat = 0;
  31. static BOOL fParseSystem = 0;
  32.  
  33. static unsigned int sIndex = 0;
  34. static char        *apszIncludeList[200];
  35.  
  36. //-------------------------------------------------------------------------
  37.  
  38. #define USAGE "usage: cinc [flags] filename\n"                      \
  39.               "\t-s  search system include files also\n" \
  40.               "\t-f  format the display output\n"
  41.  
  42. //-------------------------------------------------------------------------
  43.  
  44. int CheckFile(char *pszFileName)
  45. {
  46.    int sResult = 0;
  47.  
  48.    if (sIndex < 200)
  49.    {
  50.       for (int i = 0; i < (int)sIndex; ++i)
  51.       {
  52.          if (stricmp(pszFileName, apszIncludeList[i]) == 0)
  53.          {
  54.             sResult = 1;
  55.             break;
  56.          }
  57.       }
  58.  
  59.       if (sResult == 0)
  60.       {
  61.          //-- add the item to the list
  62.          apszIncludeList[sIndex] = new char[180];
  63.          strcpy(apszIncludeList[sIndex], pszFileName);
  64.          ++sIndex;
  65.       }
  66.    }
  67.    else
  68.    {
  69.       printf("Error: Include structure is to complex to process\n");
  70.       exit(-1);
  71.    }
  72.  
  73.    //-- only process include files not in the list
  74.    return (sResult == 0) ? TRUE : FALSE;
  75. }
  76.  
  77. //-------------------------------------------------------------------------
  78. //-- this will cause a GPF if the file is loaded from the output window
  79.  
  80. void IndentChange(void)
  81. {
  82.    if (fFormat)
  83.    {
  84.       sIndent = (sIndent < 5) ? sIndent : 5;
  85.    
  86.       switch (sIndent)
  87.       {
  88.          case 1:
  89.             strcpy(szIndent, " ");
  90.             break;
  91.    
  92.          case 2:
  93.             strcpy(szIndent, "  |- ");
  94.             break;
  95.    
  96.          case 3:
  97.             strcpy(szIndent, "  |  |- ");
  98.             break;
  99.    
  100.          case 4:
  101.             strcpy(szIndent, "  |  |  |- ");
  102.             break;
  103.    
  104.          default:
  105.             strcpy(szIndent, "  |  |  |... ");
  106.             break;
  107.       }
  108.    }
  109.    else
  110.    {
  111.         strcpy(szIndent, " ");
  112.    }
  113. }
  114.  
  115. //-------------------------------------------------------------------------
  116. //-- use this one eventually
  117. void IndentChange1(void)
  118. {
  119.    if (fFormat)
  120.    {
  121.       sIndent = (sIndent < 5) ? sIndent : 5;
  122.    
  123.       switch (sIndent)
  124.       {
  125.          case 1:
  126.             strcpy(szIndent, " ");
  127.             break;
  128.    
  129.          case 2:
  130.             strcpy(szIndent, "  |- ");
  131.             break;
  132.    
  133.          case 3:
  134.             strcpy(szIndent, "  |  |- ");
  135.             break;
  136.    
  137.          case 4:
  138.             strcpy(szIndent, "  |  |  |- ");
  139.             break;
  140.    
  141.          default:
  142.             strcpy(szIndent, "  |  |  |... ");
  143.             break;
  144.       }
  145.    }
  146.    else
  147.    {
  148.         strcpy(szIndent, " ");
  149.    }
  150. }
  151.  
  152. //-------------------------------------------------------------------------
  153.  
  154. void PrintLine(char *pszFile, char *pszLine, int sLine)
  155. {
  156.    char szFileName[150];
  157.  
  158.    //-- build up an file name with indent
  159.    strcpy(szFileName, szIndent);
  160.    strcat(szFileName, pszFile);
  161.  
  162.    if (fFormat)
  163.    {
  164.       printf("%-30.30s Line: %4.4d  |  %s", szFileName, sLine, pszLine);
  165.    }
  166.    else
  167.    {
  168.       printf("File:%-25.25s Line: %4.4d  |  %s", szFileName, sLine, pszLine);
  169.    }
  170. }
  171.  
  172. //-------------------------------------------------------------------------
  173.  
  174. void ParseFile(char *pszFileName, BOOL fLocal = 0)
  175. {
  176.    char szLine[120];
  177.  
  178.    FILE *pFile = 0;
  179.  
  180.    ++sIndent;
  181.  
  182.    //-- build up an indent string level
  183.    IndentChange();
  184.  
  185.    //-- should we try to find the file locally
  186.    if (fLocal == 0)
  187.    {
  188.       //-- try to open the file in the current directory
  189.       pFile = fopen(pszFileName, "r");
  190.    }
  191.  
  192.    //-- search the include path if we don't have a file
  193.    if (pFile == 0)
  194.    {
  195.       //-- look for the file in the INCLUDE path
  196.       _searchenv(pszFileName, "INCLUDE", szFullName);
  197.  
  198.       //-- try to open it
  199.       if (szFullName[0])
  200.       {
  201.          pFile = fopen(szFullName, "r");
  202.       }
  203.    }
  204.  
  205.    int sLine = 0;
  206.  
  207.    //-- process the file if we have one
  208.    if (pFile)
  209.    {
  210.       while (feof(pFile) == 0)
  211.       {
  212.          sLine++;
  213.  
  214.          fgets(szLine, sizeof(szLine), pFile);
  215.  
  216.          if (strncmp(szLine, "#include ", 9) == 0)
  217.          {
  218.             strcpy(szLineTemp, szLine);
  219.  
  220.             char *pszFile = strchr(szLine, '<');
  221.  
  222.             if (pszFile)
  223.             {
  224.                ++pszFile;
  225.  
  226.                //-- get the end of the file name
  227.                char *pszEnd = strchr(pszFile, '>');
  228.                
  229.                if (pszEnd)
  230.                {
  231.                   *pszEnd = '\0';
  232.  
  233.                   //-- make sure we should process syustem files
  234.                   if (fParseSystem)
  235.                   {
  236.                      //-- make sure the file has not already been processed
  237.                      if (CheckFile(pszFile))
  238.                      {
  239.                         PrintLine(pszFileName, szLineTemp, sLine);
  240.    
  241.                         ParseFile(pszFile, 0);
  242.                      }
  243.                   }
  244.                   else
  245.                   {
  246.                      // just print it out
  247.                      PrintLine(pszFileName, szLineTemp, sLine);
  248.                   }
  249.                }
  250.             }
  251.             else
  252.             {
  253.                pszFile = strchr(szLine, '\"');
  254.  
  255.                if (pszFile)
  256.                {
  257.                   ++pszFile;
  258.  
  259.                   //-- get the end of the file name
  260.                   char *pszEnd = strchr(pszFile, '\"');
  261.  
  262.                   if (pszEnd)
  263.                   {
  264.                      *pszEnd = '\0';
  265.  
  266.                      //-- make sure the file has not already been processed
  267.                      if (CheckFile(pszFile))
  268.                      {
  269.                         PrintLine(pszFileName, szLineTemp, sLine);
  270.    
  271.                         ParseFile(pszFile, 1);
  272.                      }
  273.                   }
  274.                }
  275.             }
  276.          }
  277.       }
  278.  
  279.       fclose(pFile);
  280.    }
  281.    else
  282.    {
  283.       printf("Error when opening the file '%s'\n", pszFileName);
  284.    }
  285.  
  286.    //-- keep track of the indent level
  287.    --sIndent;
  288.  
  289.    //-- put out a blank line to make the output readable
  290.    if ((fFormat) && (sIndent == 1))
  291.    {
  292.       printf("\n");
  293.    }
  294.  
  295.    //-- restore the indent string level
  296.    IndentChange();
  297. }
  298.  
  299. //-------------------------------------------------------------------------
  300.  
  301. int main(int argc, char *argv[])
  302. {
  303.    int sResult = 0;
  304.    char *pszFileName = 0;
  305.  
  306.    memset(apszIncludeList, 0, sizeof(apszIncludeList));
  307.  
  308.    if (argc == 2)
  309.    {
  310.       pszFileName = argv[1];
  311.    }
  312.    else if (argc == 3)
  313.    {
  314.       if (strlen(argv[1]) != 2)
  315.       {
  316.          printf("Unknown flag '%s'\n \n", argv[1]);
  317.          printf(USAGE);
  318.          exit(-1);
  319.       }
  320.  
  321.       if ((argv[1][1] == 's') || (argv[1][1] == 'S'))
  322.       {
  323.          fParseSystem = TRUE;
  324.  
  325.          pszFileName = argv[2];
  326.       }
  327.       else if ((argv[1][1] == 'f') || (argv[1][1] == 'F'))
  328.       {
  329.          fFormat = TRUE;
  330.  
  331.          pszFileName = argv[2];
  332.       }
  333.       else
  334.       {
  335.          printf("Unknown flag '%s'\n \n", argv[1]);
  336.          printf(USAGE);
  337.          exit(-1);
  338.       }
  339.    }
  340.    else if (argc == 4)
  341.    {
  342.       if (strlen(argv[1]) != 2)
  343.       {
  344.          printf("Unknown flag '%s'\n \n", argv[1]);
  345.          printf(USAGE);
  346.          exit(-1);
  347.       }
  348.  
  349.       if (strlen(argv[2]) != 2)
  350.       {
  351.          printf("Unknown flag '%s'\n \n", argv[1]);
  352.          printf(USAGE);
  353.          exit(-1);
  354.       }
  355.  
  356.       if ((argv[1][1] == 's') || (argv[1][1] == 'S'))
  357.       {
  358.          fParseSystem = TRUE;
  359.  
  360.          pszFileName = argv[3];
  361.       }
  362.       else if ((argv[1][1] == 'f') || (argv[1][1] == 'F'))
  363.       {
  364.          fFormat = TRUE;
  365.  
  366.          pszFileName = argv[3];
  367.       }
  368.       else
  369.       {
  370.          printf("Unknown flag '%s'\n \n", argv[1]);
  371.          printf(USAGE);
  372.          exit(-1);
  373.       }
  374.  
  375.       if ((argv[2][1] == 's') || (argv[1][1] == 'S'))
  376.       {
  377.          fParseSystem = TRUE;
  378.  
  379.          pszFileName = argv[3];
  380.       }
  381.       else if ((argv[2][1] == 'f') || (argv[1][1] == 'F'))
  382.       {
  383.          fFormat = TRUE;
  384.  
  385.          pszFileName = argv[3];
  386.       }
  387.       else
  388.       {
  389.          printf("Unknown flag '%s'\n \n", argv[1]);
  390.          printf(USAGE);
  391.          exit(-1);
  392.       }
  393.    }
  394.    else
  395.    {
  396.       printf(USAGE);
  397.       exit(-1);
  398.    }
  399.  
  400.    char szExt[_MAX_EXT];
  401.    char szDir[_MAX_DIR];
  402.    char szDrive[_MAX_DRIVE];
  403.    char szFileName[_MAX_FNAME];
  404.  
  405.    char szPathCurrent[_MAX_DRIVE];
  406.  
  407.    //-- save the current details   
  408.    int sDriveCurrent = _getdrive();
  409.    getcwd(szPathCurrent, sizeof(szPathCurrent));
  410.  
  411.    //-- change to the document directory if possible
  412.    _splitpath(pszFileName, szDrive, szDir, szFileName, szExt);
  413.  
  414.    //-- change to the disk required
  415.    if (szDrive[0] != '\0')
  416.    {
  417.       char chDrive = szDrive[0];
  418.    
  419.       //-- make sure it is upper case
  420.       toupper(chDrive);
  421.       
  422.       _chdrive(chDrive);
  423.    }
  424.  
  425.    //-- change to the dir required
  426.    if (szDir[0] != '\0')
  427.    {
  428.       //-- remove the '\' character
  429.       int sLength = strlen(szDir);
  430.       if (sLength > 3) szDir[sLength - 1] = '\0';
  431.  
  432.       //-- new directory
  433.       chdir(szDir);
  434.    }
  435.  
  436.    //-- parse the file
  437.    printf("C/C++ Include File List for '%s'\n\n", pszFileName);
  438.  
  439.    char szTempFile[_MAX_FNAME + _MAX_EXT];
  440.  
  441.    //-- only need the filename part
  442.    strcpy(szTempFile, szFileName);
  443.    strcat(szTempFile, szExt);
  444.  
  445.    ParseFile(szTempFile);
  446.  
  447.    //-- release any memory we may have allocated
  448.    for (int i = 0; i < (int)sIndex; ++i)
  449.    {
  450.       delete apszIncludeList[i];
  451.    }
  452.  
  453.    //-- restore the old details
  454.    _chdrive(sDriveCurrent);
  455.    chdir(szPathCurrent);
  456.  
  457.    return sResult;
  458. }
  459.  
  460. //-------------------------------------------------------------------------
  461.  
  462.  
  463.